#include "applicationstatemanager.h"
#include "statusbarmanager.h"
#include "customfilebrowser.h"
#include "immediateupdatestrategy.h"
#include <QDebug>
ApplicationStateManager::ApplicationStateManager(QObject* parent)
    : QObject(parent)
    , m_statusBarManager(nullptr)
    , m_fileBrowser(nullptr)
    , m_currentState(StatusBarState::idle())  // Martin Fowler重构：使用工厂方法初始化
    , m_updateStrategy(nullptr)               // Martin Fowler重构：Strategy Pattern初始化
{
}
void ApplicationStateManager::setStatusBarManager(StatusBarManager* statusBarManager)
{
    m_statusBarManager = statusBarManager;
    // Martin Fowler重构：Strategy Pattern - 自动配置默认策略
    if (!m_updateStrategy && statusBarManager) {
        setUpdateStrategy(std::make_unique<ImmediateUpdateStrategy>(statusBarManager));
    }
}
void ApplicationStateManager::setFileBrowser(CustomFileBrowser* fileBrowser)
{
    m_fileBrowser = fileBrowser;
}
// Martin Fowler重构：Replace Data Clumps - 统一状态更新实现
void ApplicationStateManager::updateCurrentFileStatus(int currentIndex, int totalFiles, const QString& fileName)
{
    StatusBarInfo newFileInfo(currentIndex, totalFiles, fileName);
    StatusBarState newState = StatusBarState::withFileInfo(m_currentState, newFileInfo);
    applyStateChange(newState);
}
void ApplicationStateManager::updatePageStatus(int currentPage, int totalPages)
{
    StatusBarState newState = StatusBarState::withPageInfo(m_currentState, currentPage, totalPages);
    applyStateChange(newState);
}
void ApplicationStateManager::updateProcessingSettings(const ProcessingSettings& settings)
{
    StatusBarState newState = StatusBarState::withSettings(m_currentState, settings);
    applyStateChange(newState);
}
void ApplicationStateManager::updateCacheStatus(qint64 cacheSize, int cacheCount)
{
    StatusBarState newState = StatusBarState::withCacheInfo(m_currentState, cacheSize, cacheCount);
    applyStateChange(newState);
}
void ApplicationStateManager::updateSystemStatus(const QString& status)
{
    StatusBarState newState = StatusBarState::withSystemStatus(m_currentState, status);
    applyStateChange(newState);
}
void ApplicationStateManager::updateExportProgress(const ExportProgress& exportProgress)
{
    StatusBarState newState = StatusBarState::withExportProgress(m_currentState, exportProgress);
    applyStateChange(newState);
}
void ApplicationStateManager::updateCompleteState(const StatusBarState& newState)
{
    applyStateChange(newState);
}
// Martin Fowler重构：Strategy Pattern - 策略管理方法
void ApplicationStateManager::setUpdateStrategy(std::unique_ptr<IStatusUpdateStrategy> strategy)
{
    if (m_updateStrategy) {
        // 刷新当前策略的挂起更新
        m_updateStrategy->flushPendingUpdates();
    }
    m_updateStrategy = std::move(strategy);
    if (m_updateStrategy) {
    }
}
// Martin Fowler重构：Extract Method - 统一状态更新逻辑
void ApplicationStateManager::applyStateChange(const StatusBarState& newState)
{
    if (!newState.isValid()) {
        return;
    }
    StatusBarState oldState = m_currentState;
    m_currentState = newState;
    // Martin Fowler重构：Strategy Pattern - 使用策略进行UI更新
    if (m_updateStrategy) {
        m_updateStrategy->applyUpdate(newState, oldState, m_statusBarManager);
    } else {
        return; // 不再提供fallback
    }
    // 发出统一状态变化信号
    emit stateChanged(newState, oldState);
}
void ApplicationStateManager::notifySpecificChanges(const StatusBarState& newState, const StatusBarState& oldState)
{
    // Martin Fowler重构：Guard Clause - 提前返回减少嵌套
    if (newState.fileInfoChanged(oldState)) {
        emit fileStatusChanged(newState.fileInfo());
    }
    if (newState.pageInfoChanged(oldState)) {
        emit pageStatusChanged(newState.currentPage(), newState.totalPages());
    }
    if (newState.cacheInfoChanged(oldState)) {
        emit cacheStatusChanged(newState.cacheSize(), newState.cacheCount());
    }
    if (newState.systemStatusChanged(oldState)) {
        emit systemStatusChanged(newState.systemStatus());
    }
    if (newState.exportProgressChanged(oldState)) {
        emit exportProgressChanged(newState.exportProgress());
    }
}